package org.springsource.roo.extrack.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import org.springsource.roo.extrack.client.managed.request.ApplicationRequestFactory;
import org.springsource.roo.extrack.client.managed.request.EmployeeProxy;
import org.springsource.roo.extrack.client.managed.request.ReportProxy;
import org.springsource.roo.extrack.client.managed.request.ReportRequest;
import org.springsource.roo.extrack.client.managed.ui.ReportDetailsView;
import org.springsource.roo.extrack.client.managed.ui.ReportEditView;
import org.springsource.roo.extrack.client.managed.ui.ReportListView;
import org.springsource.roo.extrack.client.managed.ui.ReportMobileDetailsView;
import org.springsource.roo.extrack.client.managed.ui.ReportMobileEditView;
import org.springsource.roo.extrack.client.scaffold.ScaffoldApp;
import org.springsource.roo.extrack.client.scaffold.place.CreateAndEditProxy;
import org.springsource.roo.extrack.client.scaffold.place.FindAndEditProxy;
import org.springsource.roo.extrack.client.scaffold.place.ProxyPlace;

public class ReportActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public ReportActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new ReportDetailsActivity((EntityProxyId<ReportProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? ReportMobileDetailsView.instance() : ReportDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<org.springsource.roo.extrack.client.managed.request.ReportProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<ReportProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        ReportEditView.instance().setCreating(true);
        final ReportRequest request = requests.reportRequest();
        Activity activity = new CreateAndEditProxy<ReportProxy>(ReportProxy.class, request, ScaffoldApp.isMobile() ? ReportMobileEditView.instance() : ReportEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ReportProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new ReportEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ReportMobileEditView.instance() : ReportEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        ReportEditView.instance().setCreating(false);
        EntityProxyId<ReportProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<ReportProxy>(proxyId, requests, ScaffoldApp.isMobile() ? ReportMobileEditView.instance() : ReportEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ReportProxy proxy) {
                ReportRequest request = requests.reportRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new ReportEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ReportMobileEditView.instance() : ReportEditView.instance(), activity, proxyId);
    }
}
